package com.cw.config;

import com.cw.common.api.Response;
import com.cw.common.method.CommonMethod;
import com.cw.entity.ExtInformation;
import com.cw.entity.PatientBedMiddle;
import com.cw.mapper2.ExtInformationMapper;
import com.cw.mapper2.PatientBedMiddleMapper;
import com.cw.mapper2.ThisIiPrepayinMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@EnableAsync
public class Task {
    @Autowired
    private PatientBedMiddleMapper patientBedMiddleMapper;
    @Autowired
    private ThisIiPrepayinMapper thisIiPrepayinMapper;
    private CommonMethod commonMethod = new CommonMethod();
    @Autowired
    private ExtInformationMapper extInformationMapper;
//    @Scheduled(fixedRate = 6000)
    @Scheduled(cron = "0 0 0 * * ?")
    @Async
    public Response po() {
        Date currDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ExtInformation> insertList = new ArrayList<>();
        //拿到middle表里已预约的人
        List<PatientBedMiddle> alreadyAppointmentList = patientBedMiddleMapper.getReserved();
        if (!alreadyAppointmentList.isEmpty()) {
            List<BigDecimal> preidList = alreadyAppointmentList.stream().map(PatientBedMiddle::getPreid).collect(Collectors.toList());
            patientBedMiddleMapper.updateReleaseBed(preidList);
            thisIiPrepayinMapper.updateBedNo(preidList);
            //根据Preid去查这些患者信息
            List<PatientBedMiddle> patientBedMiddles = patientBedMiddleMapper.selectPreidBed(preidList);
            //List<BigDecimal> getAllPatientsList = patientBedMiddles.stream().map(t -> t.getPreid()).collect(Collectors.toList());
            patientBedMiddles.forEach(item -> {
                ExtInformation entity = new ExtInformation();
                //消息类型
                entity.setMessageTpye("AutomaticRelease");
                entity.setExtendField1(item.getPreid() != null ? String.valueOf(item.getPreid()) : ""); //PreID
                entity.setExtendField2(item.getInpatientNo() != null ? item.getInpatientNo() : ""); //住院证号
                entity.setExtendField3(item.getNurseCellCode() != null ? item.getNurseCellCode() : "");//病区编号
                entity.setExtendField4(item.getNurseCellName() != null ? item.getNurseCellName() : "");//病区名称
                entity.setExtendField5(item.getDeptCode() != null ? item.getDeptCode() : "");   //科室代码
                entity.setExtendField6(item.getDeptName() != null ? item.getDeptName() : "");   //科室名称
                String bedNo = item.getBedNo() == null ? "" : commonMethod.getBedName(item.getBedNo() != null ? String.valueOf(item.getBedNo()) : "");
                entity.setExtendField7(bedNo);      //床位号
                entity.setExtendField8("");
                entity.setExtendField9("");
                //创建人
                entity.setCreatedBy("自动释放");
                //创建时间
                entity.setCreatedTime(sdf.format(currDate));
                //字段备注
                entity.setMessageTypeRemark("ExtendField1为PreIdExtendField2为患者住院号ExtendField3为病区代码ExtendField4为病区名称ExtendField5为科室代码ExtendField6为科室名称ExtendField7为床位号");
                insertList.add(entity);
            });
            //对EXT_INFORMATION进行插表操作
            extInformationMapper.addNonHeaderCalibration(insertList);

        } else {
            return Response.error("没有要释放的床位");
        }
        return Response.success("释放成功");
    }
    @Async
    @Scheduled(fixedRate = 600000)
    public void sfds(){
        System.out.println("第二个线程");
    }
}
